{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Polynomial interpolation\n\n\nThis example demonstrates how to approximate a function with a polynomial of\ndegree n_degree by using ridge regression. Concretely, from n_samples 1d\npoints, it suffices to build the Vandermonde matrix, which is n_samples x\nn_degree+1 and has the following form:\n\n[[1, x_1, x_1 ** 2, x_1 ** 3, ...],\n [1, x_2, x_2 ** 2, x_2 ** 3, ...],\n ...]\n\nIntuitively, this matrix can be interpreted as a matrix of pseudo features (the\npoints raised to some power). The matrix is akin to (but different from) the\nmatrix induced by a polynomial kernel.\n\nThis example shows that you can do non-linear regression with a linear model,\nusing a pipeline to add non-linear features. Kernel methods extend this idea\nand can induce very high (even infinite) dimensional feature spaces.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(__doc__)\n\n# Author: Mathieu Blondel\n#         Jake Vanderplas\n# License: BSD 3 clause\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn.linear_model import Ridge\nfrom sklearn.preprocessing import PolynomialFeatures\nfrom sklearn.pipeline import make_pipeline\n\n\ndef f(x):\n    \"\"\" function to approximate by polynomial interpolation\"\"\"\n    return x * np.sin(x)\n\n\n# generate points used to plot\nx_plot = np.linspace(0, 10, 100)\n\n# generate points and keep a subset of them\nx = np.linspace(0, 10, 100)\nrng = np.random.RandomState(0)\nrng.shuffle(x)\nx = np.sort(x[:20])\ny = f(x)\n\n# create matrix versions of these arrays\nX = x[:, np.newaxis]\nX_plot = x_plot[:, np.newaxis]\n\ncolors = ['teal', 'yellowgreen', 'gold']\nlw = 2\nplt.plot(x_plot, f(x_plot), color='cornflowerblue', linewidth=lw,\n         label=\"ground truth\")\nplt.scatter(x, y, color='navy', s=30, marker='o', label=\"training points\")\n\nfor count, degree in enumerate([3, 4, 5]):\n    model = make_pipeline(PolynomialFeatures(degree), Ridge())\n    model.fit(X, y)\n    y_plot = model.predict(X_plot)\n    plt.plot(x_plot, y_plot, color=colors[count], linewidth=lw,\n             label=\"degree %d\" % degree)\n\nplt.legend(loc='lower left')\n\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}